<?php
/**
 * 收货地址管理
 * User: tengyu
 * Date: 2017/8/29
 * Time: 10:02
 */?>
@extends('common.common')
@section('title','地址-个人中心')
@section('css')<link rel="stylesheet" href="{{asset('/css/personal_address.css')}}">@endsection
@section('js')<script src="{{asset('/js/distpicker.data.js')}}"></script>
<script src="{{asset('/js/distpicker.js')}}"></script>
<script src="{{asset('/js/main.js')}}"></script>
<script>
    /**新增收货地址*/
    function addaddr_submit(id)
    {
        /**数据校验*/
        var rename = $("#ModalBox input[name='rename']").val();
        if(!rename){
            alert('收货人不能为空!');return false;
        }
        var addr_area = $("#province1 option:selected").val();
        if(!addr_area){
            alert('收货地址省/直辖市不能为空!');return false;
        }
        var addr_city = $("#city1 option:selected").val();
        if(!addr_city){
            alert('收货地址市/直辖市郊县不能为空!');return false;
        }
        var addr_town = $("#district1 option:selected").val();
        if(!addr_town && (JSON.stringify(addr_city) != '天津市郊县' || JSON.stringify(addr_city) != '上海市郊县' || JSON.stringify(addr_city) != '重庆市郊县')){
            alert('收货郊县地址不能为空!');return false;
        }
        var addr_detail = $("#ModalBox input[name='addr_detail']").val();
        if(!addr_detail){
            alert('收货详细地址不能为空!');return false;
        }
        /**是否选作默认收货地址判断*/
        var addr_status =$("input[type='checkbox']").is(':checked');
        if(!addr_status){
            addr_status = '0';
        }else{
            addr_status = '1';
        }
        var rephone = $("#ModalBox input[name='rephone']").val();
        if(!rephone){
            alert('收货人联系不能为空!');return false;
        }
        $.ajax({
            url:'/user/addaddr/'+id,
            type:'post',
            headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' },
            data:{
                'rename':rename,'addr_area':addr_area, 'addr_city':addr_city,
                'addr_town':addr_town, 'addr_detail':addr_detail,'rephone':rephone,
                'addr_status':addr_status
            },
            dataType:'json',
            success:function(res){
                if(res.res == 'ok'){
                    alert(res.msg);
                    history.go(0);
                }else{
                    alert(res.msg);
                }
            }
        });
    }
    /**设置默认地址*/
    function default_addr(id)
    {
        $.ajax({
            url:'/user/addr/default/'+id,
            type:'get',
            dataType:'json',
            success:function(res){
                if(res.res == 'ok'){
                    alert(res.msg);
                    history.go(0);
                }else{
                    alert(res.msg);
                }
            }
        });
    }
    /**地址删除*/
    function addr_destroy(id)
    {
        $.ajax({
            url:'/user/addrdestroy/'+id,
            type:'get',
            dataType:'json',
            success:function(res){
                if(res.res == 'ok'){
                    alert(res.msg);
                    history.go(0);
                }else{
                    alert(res.msg);
                }
            }
        });
    }
</script>
@endsection
@section('content')
    <div id="content">
        <!--内容导航栏-->
        @include('common.centernav')
        <h3>个人中心</h3>
        <div class="container">
            <div class="row">
                @include('usercenter.usersidebar')
                <div class="col-xs-9">
                    <!--右侧面版-->
                    <div class="panel panel-default rightPanel">
                        <div class="panel-heading">
                            <h4>收货地址</h4>
                        </div>
                        <div class="panel-body">
                            <ul class="address">
                                @foreach($param['address'] as $v)
                                <li>
                                    <div class="border <?php if($v->addr_status == '1'){echo 'choosed';};?>">
                                        <!--圆圈-->
                                        <div class="col-xs-1"  @if($v->addr_status != '1')onclick="default_addr({{$v->addr_id}})"@endif><b></b></div>
                                        <div class="col-xs-11">
                                            <p>地址 : {{$v->addr_area}}&nbsp;{{$v->addr_city}}&nbsp;{{$v->addr_town}} &nbsp;{{$v->addr_detail}}</p>
                                            <p>姓名 : {{$v->addr_rename}}</p>
                                            <p>联系方式 : {{$v->addr_rephone}}</p>
                                            <p>
                                                <span onclick="addr_destroy({{$v->addr_id}})">删除</span>
                                            @if($v->addr_status == '1')
                                                <span class="rt">已设为默认地址</span>
                                                @else
                                                <span class="rt" onclick="default_addr({{$v->addr_id}})">设为默认地址</span>
                                            @endif
                                            </p>
                                        </div>
                                    </div>
                                </li>
                                @endforeach
                                <!--添加新地址-->
                                <li id="addAddress">
                                    <div class="border">
                                        <div data-toggle="modal" data-target="#ModalBox">+</div>
                                        <p>新增收货地址</p>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div id="partition"></div>
    <!--修改地址模态框-->
    <div id="ModalBox" class="modal" data-backdrop="static">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <span class="close" data-dismiss="modal">&times;</span>
                    <h4 class="modal-title">新增收货地址</h4>
                </div>
                <div class="modal-body">
                    <form class="form-inline">
                        <div class="form-group">
                            <label class="control-label">收货人:</label>
                            <input type="text" class="form-control" name="rename" value="{{old('rename')}}" placeholder="请输入收货人姓名">
                        </div>
                        <div class="form-group">
                            <label id="district" class="control-label" >所在地区:</label>
                            <div id="selectAdd" data-toggle="distpicker">
                                <div class="form-group">
                                    <label class="sr-only" for="province1">Province</label>
                                    <select class="form-control" id="province1"></select>
                                </div>
                                <div class="form-group">
                                    <label class="sr-only" for="city1">City</label>
                                    <select class="form-control" id="city1"></select>
                                </div>
                                <div class="form-group">
                                    <label class="sr-only" for="district1">District</label>
                                    <select class="form-control" id="district1"></select>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label">详细地址:</label>
                            <input type="text" class="form-control" name="addr_detail" value="{{old('addr_detail')}}" placeholder="请输入详细地址">
                        </div>
                        <div class="form-group">
                            <label class="control-label">联系方式:</label>
                            <input type="text" class="form-control" name="rephone" value="{{old('rephone')}}" placeholder="请输入收货人联系方式">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning lf" data-dismiss="modal" onclick="addaddr_submit({{$user->id}})">提交</button>
                    <input type="checkbox" name="status">&nbsp;设置为默认地址
                </div>
            </div>
        </div>
    </div>
@endsection
